package refactor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import common.bean.DeviceBean;
import common.dto.RealStatusDto;
import common.standard.table.DeviceFieldEnum;
import common.standard.table.PageEnum;
import common.util.ConditionUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import refactor.entity.Device;
import refactor.mapper.DeviceMapper;
import refactor.service.IDeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 记录各个系统模块设备的相关信息 服务实现类
 * </p>
 */
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements IDeviceService {

    @Autowired
    DeviceMapper deviceMapper;

    @Override
    public int lotInsert(List<Device> devList) {
        return 0;
    }

    @Override
    public IPage<Device> pageList(Integer pageNum, Integer modId, Integer devId, Integer facId, String condition, DeviceBean info) {
        Page<Device> devicePage = new Page<>(pageNum, PageEnum.PAGE_SIZE.getValue());
        QueryWrapper<Device> groupQueryWrapper = new QueryWrapper<>();

        ConditionUtil.addDeviceModId(groupQueryWrapper, info);
        if(info.getBangding0()!=null){
                groupQueryWrapper.eq("facilities_id",0);
        }else if(info.getBangding1()!=null){
            groupQueryWrapper.ne("facilities_id",0);
        }

        if(devId!=0){
            groupQueryWrapper.eq(DeviceFieldEnum.ID.getFieldName(), devId);
        }
        if(facId!=0){
            groupQueryWrapper.eq(DeviceFieldEnum.FACILITIES_ID.getFieldName(),facId);
        }
        if(!condition.equals("")){
            groupQueryWrapper.like(DeviceFieldEnum.CODE.getFieldName(),condition).or().like(DeviceFieldEnum.NAME.getFieldName(),condition);
        }
        return deviceMapper.selectPage(devicePage, groupQueryWrapper);
    }

    @Override
    public HashMap<String, RealStatusDto> getreadData(List<Integer> eleIds, List<Integer> waterIds, List<Integer> menjinIds, List<Integer> yanganIds) {
        HashMap<String, RealStatusDto> res=new HashMap<>(10);
      //  List<RealStatusDto> res=new ArrayList<RealStatusDto>();
        if(eleIds.size()>0){
            List<Map<String, Object>> eleRead = deviceMapper.getEleRead(eleIds);
            for(Map<String,Object> m:eleRead){
                res.put(m.get("id").toString(),new RealStatusDto(m.get("id").toString(),m.get("eleData").toString(),(Long)m.get("reportTime")));
            }
        }
       if(waterIds.size()>0){
           List<Map<String, Object>> waterRead = deviceMapper.getWaterRead(waterIds);
           for(Map<String,Object> m:waterRead){
               res.put(m.get("id").toString(),new RealStatusDto(m.get("id").toString(),m.get("waterData").toString(),(Long)m.get("reportTime")));
           }
       }
        if(menjinIds.size()>0){
            List<Map<String, Object>> waterRead = deviceMapper.getMenjinRead(menjinIds);
            for(Map<String,Object> m:waterRead){
                res.put(m.get("id").toString(),new RealStatusDto(m.get("id").toString(),(Long)m.get("reportTime")));
            }
        }
        if(yanganIds.size()>0){
            List<Map<String, Object>> waterRead = deviceMapper.getWaterRead(yanganIds);
            for(Map<String,Object> m:waterRead){
                res.put(m.get("id").toString(),new RealStatusDto(m.get("id").toString(),(Long)m.get("reportTime")));
            }
        }

        return res;
    }


}
